package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MyInfoBeeean;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CardActtivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.cardname)
    EditText cardname;
    @BindView(R.id.cardcode)
    EditText cardcode;
    @BindView(R.id.bnt)
    TextView bnt;

    String userId;
    String headImg;
    String nicknamecanshu;
    String userName;
    String userIdcard;
    String userSex;
    String qqnum;
    String birthday;
    String province;
    String citycanshu;
    String state;

    @Override
    protected int initLayout() {
        return R.layout.activity_card;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        userId = (String) SPUtils.get(thisActivity, "userid", "");
        userName=getIntent().getStringExtra("name");
        state=getIntent().getStringExtra("state");
        userIdcard=getIntent().getStringExtra("cardid");
        cardname.setText(userName);
        cardcode.setText(userIdcard);
        if(state.equals("未申请")){
            bnt.setVisibility(View.VISIBLE);
        }else if(state.equals("审核中")){
            bnt.setVisibility(View.GONE);
        }else if(state.equals("审核通过")){
            bnt.setVisibility(View.GONE);
        }else if(state.equals("审核不通过")){
            bnt.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void initEvent() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }



    @OnClick(R.id.bnt)
    public void onViewClicked() {
        if(EmptyUtil.isEmpty(cardname.getText().toString())){
            ToastUtils.showShort(thisActivity,"请输入真实姓名");
            return;
        }

        if(EmptyUtil.isEmpty(cardcode.getText().toString())){
            ToastUtils.showShort(thisActivity,"请输入身份证号");
            return;
        }
        userName=cardname.getText().toString();
        userIdcard=cardcode.getText().toString();
        MyInfo(userId,
                headImg,
                nicknamecanshu,
                userName,
                userIdcard,
                userSex,
                qqnum,
                birthday,
                province,
                citycanshu
        );


    }

    /**
     * 修改信息
     */
    public void MyInfo(String userId,
                       String headImg,
                       String nickname,
                       String userName,
                       String userIdcard,
                       String userSex,
                       String qqnum,
                       String birthday,
                       String province,
                       String city) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .updateUser(userId,
                        headImg,
                        nickname,
                        userName,
                        userIdcard,
                        userSex,
                        qqnum,
                        birthday,
                        province,
                        city)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this, false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("xiugaixinxi", JsonStr + "");
                        Gson gson = new Gson();
                        MyInfoBeeean myInfoBeeean = gson.fromJson(JsonStr, MyInfoBeeean.class);

                        ToastUtils.showShort(thisActivity,myInfoBeeean.getMsg());
                        thisActivity.finish();

                    }
                });

    }
}
